//TODO: dziwna obsluga zmiennych fileCont, fileNPC itd. (nie ma create i free?)

unit untObjects;

interface

uses
  SysUtils, Windows, Classes, Graphics, untItems, untMech;

type
  PBitmap = ^TBitmap;

  TObiekt = class
    X,Y: Integer;
    Tag: Integer;
    Szablon: Integer;
    constructor Create; virtual;
    destructor Destroy; override;
  end;
  PObiekt = ^TObiekt;

  TContainer = class(TObiekt)
    Contains: TEquipment;
    Remains: Boolean;
    constructor Create; override;
    destructor Destroy; override;
  end;
  PContainer = ^TContainer;

  TNPC = class(TObiekt)
    Core: TCreature;
    Dialog: String;
    constructor Create; override;
    destructor Destroy; override;
  end;
  PNPC = ^TNPC;

{  TStatic = class(TObiekt)
    Block: boolean;
    constructor Create; override;
    destructor Destroy; override;
  end;
  PStatic = ^TStatic;
}

  TSzablon = object
    Name: String[20];
    Img: TBitmap;
    Typ: Integer;
    Block: Boolean;
    Desc: String;
  end;

var
  n_obj, n_sza: Integer;
  fileTemp, fileCont, fileNPC: TStringList;

procedure SetObjects;
procedure ShowObjects;
procedure AddObject(Szablon, Tag, X, Y: Integer);
procedure RemoveObject(Index: Integer);

procedure SetTemplates;
procedure FreeTemplates;

implementation

uses untMain, untMap;

constructor TObiekt.Create;
begin
end;

destructor TObiekt.Destroy;
begin
  //Obraz:= nil;
  inherited;
end;

{procedure TObiekt.InitBitmap(Image: PBitmap);
begin
  Obraz:= Image;
end;}

constructor TContainer.Create;
begin
  inherited create;
end;

destructor TContainer.Destroy;
begin
  inherited Destroy;
end;

constructor TNPC.Create;
begin
  inherited Create;
  Core:= TCreature.Create;
end;

destructor TNPC.Destroy;
begin
  Core.Free;
  inherited Destroy;
end;

{constructor TStatic.Create;
begin
  inherited Create;
end;

destructor TStatic.Destroy;
begin
  inherited Destroy;
end;}

{**********
* Obiekty *
**********}

procedure SetObjects;
  procedure StrToObj(var Obj: TObiekt; Str: String);
  var
    sTag, sSzablon, i: Integer;
    Temp: String;
  begin
    Delete(Str, Pos(' ', Str), Length(Str)-Pos(' ', Str)+1); //komentarze
    sSzablon:=StrToInt(Copy(Str, 1, Pos(';', Str)-1));
    Delete(Str, 1, Pos(';', Str));
    sTag:=StrToInt(Copy(Str, 1, Pos(';', Str)-1));
    case untMain.Szablony[sSzablon].Typ of
       STATIC: Obj:=TObiekt.Create;
       NPC:
       begin
         Obj:=TNPC.Create;
         for i:=0 to fileCont.Count-1 do
         begin
           if fileCont.Strings[i][1]='%' then
             if StrToInt(Copy(fileCont.Strings[i], 3, Length(fileCont.Strings[i])-2))=sTag then Break;
         end;
         //if fileCont.Strings[i][2]='t' then (Obj as TNPC).Core.Equipment.Remains:=true else (Obj as TNPC).Core.Equipment.Remains:=false;
         Inc(i);
         Temp:=fileCont.Strings[i];
         repeat
           (Obj as TNPC).Core.Equipment.AddItem(
              @Items.Items[Items.FindItem(Copy(Temp, 1, Pos(',', Temp)-1))], StrToInt(Copy(Temp, Pos(',', Temp)+1, Length(Temp))));
           Inc(i);
           Temp:=fileCont.Strings[i];
         until (Temp[1]='%') or (i=fileCont.Count);
         (Obj as TNPC).Dialog:=fileNPC[sTag];
       end;
       CONTAINER:
       begin
         Obj:=TContainer.Create;
         for i:=0 to fileCont.Count-1 do
         begin
           if fileCont.Strings[i][1]='%' then
             if StrToInt(Copy(fileCont.Strings[i], 3, Length(fileCont.Strings[i])-2))=sTag then Break;
         end;
         if fileCont.Strings[i][2]='t' then (Obj as TContainer).Remains:=true else (Obj as TContainer).Remains:=false;
         Inc(i);
         Temp:=fileCont.Strings[i];
         repeat
           (Obj as TContainer).Contains.AddItem(
              @Items.Items[Items.FindItem(Copy(Temp, 1, Pos(',', Temp)-1))], StrToInt(Copy(Temp, Pos(',', Temp)+1, Length(Temp))));
           Inc(i);
           Temp:=fileCont.Strings[i];
         until (Temp[1]='%') or (i=fileCont.Count);
       end;
    end;
    Delete(Str, 1, Pos(';', Str));
    Obj.X:=StrToInt(Copy(Str, 1, Pos(';', Str)-1));
    Delete(Str, 1, Pos(';', Str));
    Obj.Y:=StrToInt(Copy(Str, 1, Length(Str)));
    Obj.Szablon:=sSzablon;
//    Delete(Str, 1, Pos(';', Str));
//    Obj.Tag:=StrToInt(Copy(Str, 1, Length(Str)-1));
  end;
var
  i: Integer;
begin
  n_obj:=0;
  fileCont:=TStringList.Create;
  fileCont.LoadFromFile('.\data\cont.txt');
  fileNPC:=TStringList.Create;
  fileNPC.LoadFromFile('.\data\npc.txt');
  fileTemp:=TStringList.Create;
  fileTemp.LoadFromFile('.\data\map0_0.txt');
  repeat
    fileTemp.Delete(0);
  until fileTemp.Strings[0]='<OBJ>';
  fileTemp.Delete(0);

  for i:=0 to fileTemp.Count-1 do
  begin
    StrToObj(Obiekty[i], fileTemp[i]);
    ObiektyI[Obiekty[i].X, Obiekty[i].Y]:=i+1;
    Inc(n_obj);
  end;

  fileTemp.Free;
  fileNPC.Free;
  fileCont.Free;
end;

procedure ShowObjects;
var
  X,Y: Integer;
begin
  for Y:=0 to 23 do
    for X:=0 to 31 do
    begin
      if ObiektyI[X+PosX-(XAMOUNT div 2),Y+PosY-(YAMOUNT div 2)]>0 then
      begin
         TransparentBlt(Tiles.Canvas.Handle,
                 XSIZE*(Obiekty[ObiektyI[X+PosX-(XAMOUNT div 2),Y+PosY-(YAMOUNT div 2)]-1].X-PosX+(XAMOUNT div 2)), YSIZE*(Obiekty[ObiektyI[X+PosX-(XAMOUNT div 2),Y+PosY-(YAMOUNT div 2)]-1].Y-PosY+(YAMOUNT div 2)),
                 XSIZE, YSIZE,
                 Szablony[Obiekty[ObiektyI[X+PosX-(XAMOUNT div 2),Y+PosY-(YAMOUNT div 2)]-1].Szablon].Img.Canvas.Handle,
                 0, 0,
                 XSIZE, YSIZE,
                 RGB(255, 255, 255));

         {Tiles.Canvas.Brush.Style:=bsClear;
         Tiles.Canvas.Font.Color:=clBlack;
         Tiles.Canvas.Font.Style:=[];
         Tiles.Canvas.TextOut(XSIZE*(Obiekty[ObiektyI[X+PosX,Y+PosY]-1].X-PosX), YSIZE*(Obiekty[ObiektyI[X+PosX,Y+PosY]-1].Y-PosY), Szablony[Obiekty[ObiektyI[X+PosX,Y+PosY]-1].Tag].Name);}
      end;
  end;
end;

procedure AddObject(Szablon, Tag, X, Y: Integer);
begin
  Inc(n_obj);
  case Szablony[Szablon].Typ of
    CONTAINER: Obiekty[n_obj]:=TContainer.Create;
    STATIC: Obiekty[n_obj]:=TObiekt.Create;
    NPC: Obiekty[n_obj]:=TNPC.Create;
  end;
  ObiektyI[X,Y]:=n_obj+1;
  Obiekty[n_obj].Szablon:=Szablon;
  Obiekty[n_obj].Tag:=Tag;
  Obiekty[n_obj].X:=X;
  Obiekty[n_obj].Y:=Y;
end;

procedure RemoveObject(Index: Integer);
begin
  ObiektyI[Obiekty[Index].X, Obiekty[Index].Y]:=0;
  DrawAll(PosX, PosY);
end;

{***********
* Szablony *
***********}

procedure SetTemplates;
var
  i: Integer;
  Temp: String;
begin
  n_sza:=0;
  fileTemp:=TStringList.Create;
  fileTemp.LoadFromFile(TMPL_FILE);

  {i:=0;
  while i<fileTemp.Count-1 do
  begin
    Szablony[i div 3].Name:=fileTemp.Strings[i];
    Szablony[i div 3].Typ:=StrToInt(fileTemp.Strings[i+1]);
    Szablony[i div 3].Img:=TBitmap.Create;
    Szablony[i div 3].Img.LoadFromFile(fileTemp.Strings[i+2]);
    Inc(n_sza);
    i:=i+3;
  end;}
  for i:=0 to fileTemp.Count-1 do
  begin
    Temp:=fileTemp.Strings[i];
    Szablony[i].Name:=Copy(Temp, 1, Pos(';', Temp)-1);
    Delete(Temp, 1, Pos(';', Temp));
    Szablony[i].Img:=TBitmap.Create;
    Szablony[i].Img.LoadFromFile(Copy(Temp, 1, Pos(';', Temp)-1));
    Delete(Temp, 1, Pos(';', Temp));
    Szablony[i].Typ:=StrToInt(Copy(Temp, 1, Pos(';', Temp)-1));
    Delete(Temp, 1, Pos(';', Temp));
    Szablony[i].Desc:=Copy(Temp, 1, Pos(';', Temp)-1);
    Delete(Temp, 1, Pos(';', Temp));
    //Szablony[i].Block:=boolean(integer(Copy(Temp, 1, Length(Temp))));
    if Temp[1]='1' then Szablony[i].Block:=true else Szablony[i].Block:=false;
  end;

  fileTemp.Free;
end;

procedure FreeTemplates;
var
  i: Integer;
begin
  for i:=0 to n_sza do
  begin
    Szablony[i].Img.Free;
  end;
end;

end.
